<?php defined('SYSPATH') or die('No direct script access.');

/*
 * AJAX CALLS
 */

class Controller_Ajax extends Controller_Initialize {

  public $secure_actions = array(
      // user actions
      'art_cat_add' => 'admin',
      );

  public $auto_render = false;


  /*
   * Přidává novou kategorii ke článkům
   */
  public function action_articles_cat_add() {

    if (!empty($_POST)) {
      $model = null;
      $return_arr = array();

      if (! DB::query(Database::SELECT, "SELECT id FROM articles_categories WHERE name = '".$_POST['new_category']."'")->execute()->current()) {
        $model = ORM::factory('articles_category');
        $post = $model->validate_art_cat_add($_POST);

        if ($post->check()) {
          $model->name = $post['new_category'];
          $model->save();

          if ($model->saved()) {
            $return_arr["check"] = 'ok';
            $return_arr["id"] = $model->id;
            $return_arr["name"] = $model->name;
          } else {
            $return_arr["check"] = 'false';
          }
        } else {
          $return_arr["check"] = 'false';
        }        
      } else {
        $return_arr["check"] = 'false';
      }
      echo json_encode($return_arr);
    }
  }

  public function action_search_tag() {

    $q = strtolower($_GET["term"]);
    if (!$q) return;
    $query = DB::query(Database::SELECT, "SELECT tag FROM articles_tags WHERE tag LIKE '".$q."%'")->execute();

    $array = array();
    foreach ($query as $item) {
      $array[] = $item['tag'];
    }

    echo json_encode($array);

  }

  public function action_search_photos_name() {

    $q = strtolower($_GET["term"]);
    if (!$q) return;
    $query = DB::query(Database::SELECT, "SELECT id, title, thumb, date FROM photo_users WHERE title LIKE '".$q."%' AND username = '".$this->userAuth->username."'")->execute();

    $array = array();
    $i = 0;
    foreach ($query as $item) {
      foreach ($item as $key => $val) {
      $array[$i][$key] = $val;
    }
      $i++;
    }

    header('content-type: application/json');

    echo json_encode($array);

  }

  public function action_search_photos_group() {

    $id = strtolower($_GET["id"]);
    if (!$id) return;
    $photo_user = ORM::factory('photo_user', $id);

    $array = array();
    $i = 0;
    foreach ($photo_user->photo_source->find_all() as $item) {
      $array[$i]['id'] = $item->id;
      $array[$i]['filename'] = $item->filename;
      $i++;
    }

    header('content-type: application/json');

    echo json_encode($array);

  }

  public function action_photo_to_set() {

    if (!empty($_GET)) {
      $photo_set_id = ereg_replace("[^0-9]", "", $_GET['toSetId']);
      $photo_set = ORM::factory('photo_set', $photo_set_id);
      $photo_source = ORM::factory('photo_source', $_GET['id']);
      if (! $photo_set->has('photo_source',$photo_source)) {
        $photo_set->add('photo_source',$photo_source);
        $photo_set->save();
      }

      if ($photo_set->saved()) {
        $array[] = 'ok';
        header('content-type: application/json');
        echo json_encode($array);
      } else {
        echo 'fu';
      }

    }
  }

 } // End